#include "Material.h"

// CoreLib.
#include "ObjectVisitor.h"

// Renderer.
#include "ShaderProgram.h"

// GameEngine.
#include "Binding.h"

le::Material::Material()
: m_ShaderProgram(0)
{

}

le::Material::~Material()
{

}

bool le::Material::LoadFromFile( const le::Path& path )
{
	le::ObjectVisitor objVisitor;
	if (objVisitor.Load(path.ToChar()))
	{
		if(objVisitor.EnterNode("base"))
		{
			std::string vertexPath;
			std::string fragmentPath;
			objVisitor.Visit("VertexPath", vertexPath, "");
			objVisitor.Visit("FragmentPath", fragmentPath, "");

			vertexPath = le::Path::GetAbsolutePath(le::Path(vertexPath));
			fragmentPath = le::Path::GetAbsolutePath(le::Path(fragmentPath));

			le::Renderer* renderer = le::Binding::GetInstance()->GetRenderer();
			m_ShaderProgram = renderer->CreateShader(le::Path(vertexPath), le::Path(fragmentPath));

			return true;
		}
	}
	
	return false;
}

bool le::Material::SetShaderVariable( const char* variableName, const float* mat4x4 )
{
	if (m_ShaderProgram)
	{
		return m_ShaderProgram->SetShaderVariable(variableName, mat4x4);
	}
	return false;
}

uint32 le::Material::GetShaderProgramNumber() const
{
	return m_ShaderProgram->GetShaderProgramID();
}
